//
//  LockView.m
//  手机屏幕锁的实现
//
//  Created by hyl on 16/6/28.
//  Copyright © 2016年 hyl. All rights reserved.
//

#import "LockView.h"

@interface LockView ()
@property (nonatomic,strong) NSMutableArray *selectedBtnArrM;
@property (nonatomic,assign) CGPoint curP;
@end

@implementation LockView

-(NSMutableArray *) selectedBtnArrM{
    if (_selectedBtnArrM==nil) {
        _selectedBtnArrM=[NSMutableArray array];
    }
    return _selectedBtnArrM;
}

#pragma mark - setPan
-(void) setPan{
    UIPanGestureRecognizer *pan=[[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
    [self addGestureRecognizer:pan];
}
-(void) pan:(UIPanGestureRecognizer *) pan{
    _curP=[pan locationInView:self];
    for (UIButton *btn in self.subviews) {
        if (CGRectContainsPoint(btn.frame ,_curP)) {
            btn.selected=YES;
            [self.selectedBtnArrM addObject:btn];
        }
    }
    
    [self setNeedsDisplay];
    
    if (pan.state==UIGestureRecognizerStateEnded) {
        //不行???
//        [self.selectedBtnArrM makeObjectsPerformSelector:@selector(setSelected:) withObject:@(NO)];
        for (UIButton *btn in self.selectedBtnArrM) {
            btn.selected=NO;
        }
        [self.selectedBtnArrM removeAllObjects];
    }
}

#pragma mark - drawRect
- (void)drawRect:(CGRect)rect {
    // 没有选中按钮，不需要连线
    if (self.selectedBtnArrM.count == 0) return;
    
    UIBezierPath *path=[UIBezierPath bezierPath];
    
    for (int i=0; i<self.selectedBtnArrM.count-1; i++) {
        UIButton *btn=self.selectedBtnArrM[i];
        if (i==0) {
            //起点
            [path moveToPoint:btn.center];
        }else{
            [path addLineToPoint:btn.center];
        }
    }
    
    [path addLineToPoint:self.curP];
    [[UIColor greenColor]set];
    path.lineWidth=10;
    path.lineJoinStyle=kCGLineJoinRound;
    [path stroke];
}

#pragma mark - awakeFromNib
-(void) awakeFromNib{
    //代码添加与图形上添加手势不可同时存在，如：当拉了一个pan手势时，没有添加响应方法，转而代码实现，会有问题（以验证）
    [self setPan];
//    _selectedBtnArrM=[NSMutableArray array];
    for (int i=0; i<9; i++) {
        UIButton *btn=[UIButton buttonWithType:UIButtonTypeCustom];
        //醉了
//        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
//        [btn setBackgroundImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        
        [btn setImage:[UIImage imageNamed:@"gesture_node_normal"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"gesture_node_highlighted"] forState:UIControlStateSelected];
        [btn setUserInteractionEnabled:NO];
        [self addSubview:btn];
    }
    
}

#pragma mark -layoutSubviews
-(void)layoutSubviews{
    [super layoutSubviews];
    CGFloat marginW=10;
    CGFloat marginH=84;
    NSInteger rowCounts=3;
//    NSLog(@"%f",self.bounds.size.width);
    CGFloat spaceW=(self.bounds.size.width-2*marginW-rowCounts*74)/(rowCounts-1);
    
    
    NSInteger counts=self.subviews.count;
    for (int m=0; m<counts; m++) {
//        NSLog(@"%i  %i",(m%3),(m/3));
        //NSLog(@"%f  -  %f ",marginW+(m%3)*spaceW,100+(m/3)*marginH);
        UIButton *btn=self.subviews[m];
        btn.frame=CGRectMake(marginW+(m%3)*(spaceW+74), 100+(m/3)*(marginH+74), 74, 74);
    }
}
@end
